package cn.youqu.happy.module.youqu.controller.admin.userActivity;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userActivity.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userActivity.UserActivityDO;
import cn.youqu.happy.module.youqu.convert.userActivity.UserActivityConvert;
import cn.youqu.happy.module.youqu.service.userActivity.UserActivityService;

@Api(tags = "管理后台 - 用户发起活动")
@RestController
@RequestMapping("/youqu/user-activity")
@Validated
public class UserActivityController {

    @Resource
    private UserActivityService userActivityService;

    @PostMapping("/create")
    @ApiOperation("创建用户发起活动")
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:create')")
    public CommonResult<Integer> createUserActivity(@Valid @RequestBody UserActivityCreateReqVO createReqVO) {
        return success(userActivityService.createUserActivity(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户发起活动")
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:update')")
    public CommonResult<Boolean> updateUserActivity(@Valid @RequestBody UserActivityUpdateReqVO updateReqVO) {
        userActivityService.updateUserActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户发起活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:delete')")
    public CommonResult<Boolean> deleteUserActivity(@RequestParam("id") Integer id) {
        userActivityService.deleteUserActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户发起活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:query')")
    public CommonResult<UserActivityRespVO> getUserActivity(@RequestParam("id") Integer id) {
        UserActivityDO userActivity = userActivityService.getUserActivity(id);
        return success(UserActivityConvert.INSTANCE.convert(userActivity));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户发起活动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:query')")
    public CommonResult<List<UserActivityRespVO>> getUserActivityList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserActivityDO> list = userActivityService.getUserActivityList(ids);
        return success(UserActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户发起活动分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:query')")
    public CommonResult<PageResult<UserActivityRespVO>> getUserActivityPage(@Valid UserActivityPageReqVO pageVO) {
        PageResult<UserActivityDO> pageResult = userActivityService.getUserActivityPage(pageVO);
        return success(UserActivityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户发起活动 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-activity:export')")
    @OperateLog(type = EXPORT)
    public void exportUserActivityExcel(@Valid UserActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserActivityDO> list = userActivityService.getUserActivityList(exportReqVO);
        // 导出 Excel
        List<UserActivityExcelVO> datas = UserActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户发起活动.xls", "数据", UserActivityExcelVO.class, datas);
    }

}
